//
//  BindTelViewController.m
//  Kjs-iOS
//
//  Created by 江伟 on 16/6/22.
//  Copyright © 2016年 jmhz. All rights reserved.
//

#import "BindTelViewController.h"
#import "AppDelegate.h"
#import "KJSRequest.h"
#import "UIColor+KjsColor.h"
#import "CommonUtil.h"
#define CodeTimeInterval 180

@interface BindTelViewController ()
@property (nonatomic,weak) IBOutlet UITextField *phoneNumTF;
@property (nonatomic,weak) IBOutlet UITextField *codeTF;
@property (nonatomic,weak) IBOutlet UIButton *bindBtn;
@property (nonatomic,weak) IBOutlet UIButton *codeBtn;

@property (nonatomic,assign) int secondsCountDown;//倒计时总时长
@property (nonatomic,strong) NSTimer *countDownTimer;
@property (nonatomic,strong) NSString *oRepresentName;
@end

@implementation BindTelViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.bindBtn.layer.cornerRadius = 10;
    self.codeBtn.layer.cornerRadius = 15;
    self.secondsCountDown = CodeTimeInterval;
    
    if ([self.bindType isEqualToString:@"represent"]) {
        [self.navigationItem setTitle:@"验证法人手机号"];
        
    }else{
        [self.navigationItem setTitle:@"手机绑定"];
    }
    [self.bindBtn addTarget:self action:@selector(bindNewPhone) forControlEvents:UIControlEventTouchUpInside];
    [self.codeBtn addTarget:self action:@selector(sendMessage) forControlEvents:UIControlEventTouchUpInside];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self initConfigeration];
    if ([self.bindType isEqualToString:@"represent"]){
        [KJSRequest GetORealAuthInfoWithPartyId:CurentDelegate.account.partyId ret:^(ORealAuthResultModel *model) {
            self.oRepresentName = model.realAuthInfo.oRepresentName;
            self.phoneNumTF.enabled = NO;
            self.phoneNumTF.text = model.realAuthInfo.oRepresentPhone;
        }];
    }else{
        self.phoneNumTF.enabled = YES;
    }
}
#pragma mark - configeration
-(void)initConfigeration
{
    self.phoneNumTF.text = @"";
    self.codeTF.text = @"";
    self.bindBtn.enabled = NO;
    [self.bindBtn setBackgroundColor:[UIColor lightGrayColor]];
}
#pragma mark - custom method
-(void)sendMessage
{
    self.bindBtn.enabled = YES;
    [self.bindBtn setBackgroundColor:[UIColor kjsMainColor]];
    if ([self.phoneNumTF.text isEqualToString:@""]) {
        [GUATool showDialog:@"请输入手机号" message:nil waitingTime:1.5];
    }else if (![CommonUtil validateMobile:self.phoneNumTF.text]) {
        [GUATool showDialog:@"请输入正确的手机号" message:nil waitingTime:1.5];
    }else{
        if ([self.bindType isEqualToString:@"represent"]) {
            //机构用户验证法人手机号发送短信
            [KJSRequest vaildOrgRepresentSendMessageWithPhone:self.phoneNumTF.text sendName:self.oRepresentName ret:^(BasicResult *model) {
                if ([model.messageresult isEqualToString:@"0"]) {
                    [GUATool showDialog:@"验证码已发送" message:nil waitingTime:1.5];
                    self.countDownTimer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(timeFireMethod) userInfo:nil repeats:YES];
                }
            }];
        }else{
            [KJSRequest BindTelSendMessageWithPhoneNumber:self.phoneNumTF.text ret:^(BasicResult *model) {
                if ([model.messageresult isEqualToString:@"0"]) {
                    [GUATool showDialog:@"验证码已发送" message:nil waitingTime:1.5];
                    self.countDownTimer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(timeFireMethod) userInfo:nil repeats:YES];
                }
            }];
        }
    }
}
-(void)bindNewPhone
{
    
    if ([self.phoneNumTF.text isEqualToString:@""]) {
        [GUATool showDialog:@"请输入手机号" message:nil waitingTime:1.5];
    }else if (![CommonUtil validateMobile:self.phoneNumTF.text]) {
        [GUATool showDialog:@"请输入正确的手机号" message:nil waitingTime:1.5];
    }else{
        if ([self.bindType isEqualToString:@"represent"]) {
            //机构用户验证法人手机号
            [KJSRequest vaildOrgrepresentWithVerificationCode:self.codeTF.text PartyId:CurentDelegate.account.partyId ret:^(BasicResult *model) {
                if ([model.MessageAccept isEqualToString:@"success"]) {
                    [GUATool showDialog:@"认证成功" message:nil waitingTime:1.5];
                    if ([CurentDelegate.account.levels isEqualToString:@"1"]) {
                        CurentDelegate.account.levels = @"2";
                    }
                    [self dismiss:YES];
                }
            }];
        }else{
            [KJSRequest BindTelWithUserLoginId:self.phoneNumTF.text verificationCode:self.codeTF.text ret:^(BasicResult *model) {
                if ([model.returnMsg isEqualToString:@"success"]){
            UIAlertView *alertV = [[UIAlertView alloc] initWithTitle:@"手机号码更换成功，请重新登录" message:nil delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
            [alertV show];
                }
            }];
        }
    }
}
-(void)timeFireMethod{
    
    self.secondsCountDown--;
    self.codeBtn.titleLabel.text = [NSString stringWithFormat:@"%d秒",self.secondsCountDown];
    [self.codeBtn setTitle:[NSString stringWithFormat:@"%d秒",self.secondsCountDown] forState:UIControlStateNormal];
    self.codeBtn.enabled = NO;
    if (self.secondsCountDown == 0) {
        [self.countDownTimer invalidate];
        [self.codeBtn setTitle:@"获取验证码" forState:UIControlStateNormal];
        self.codeBtn.enabled = YES;
        self.secondsCountDown = CodeTimeInterval;
    }
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
    if (buttonIndex == 0){
        [self bindSuccessReturn];
    }
}
-(void)bindSuccessReturn{
    if (self.bindSuccess) {
        self.bindSuccess();
    }
    [self dismiss:YES];
}

@end
